package com.xayy.health.mgt.service.impl;

import com.xayy.health.mgt.mapper.TaskChildInfoMapper;
import com.xayy.health.mgt.mapper.TaskInfoMapper;
import com.xayy.health.mgt.model.TaskChildInfo;
import com.xayy.health.mgt.model.TaskInfo;
import com.xayy.health.mgt.model.dto.ChildTaskDto;
import com.xayy.health.mgt.model.dto.DataFeatureDto;
import com.xayy.health.mgt.service.DownloadImagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据处理-批量处理-图片下载
 * @author shichenlei
 */
@Service
@Slf4j
public class DownloadImagesServiceImpl implements DownloadImagesService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;

    @Autowired
    private TaskChildInfoMapper taskChildInfoMapper;

    /**
     * 获取下载图片
     * @return List<DataFeatureDto>
     */
    @Override
    public List<DataFeatureDto> taskList(){
        List<DataFeatureDto> dataFeatureDtoList=new ArrayList<>();
        Example example=new Example(TaskInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type",1);
        criteria.andEqualTo("isBatch",0);
        List<TaskInfo> taskInfoList=taskInfoMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(taskInfoList)){
            for(TaskInfo taskInfo:taskInfoList){
                ChildTaskDto childTaskDto=new ChildTaskDto();
                childTaskDto.setTaskId(taskInfo.getId());
                List<TaskChildInfo> childTaskPageList = taskChildInfoMapper.getChildTaskPageList(childTaskDto);
                if(!CollectionUtils.isEmpty(childTaskPageList)){
                     for(TaskChildInfo taskChildInfo:childTaskPageList){
                         if(null==taskChildInfo.getDataSituation()||"无".equals(taskChildInfo.getDataSituation())){

                                  continue;
                         }
                         String params=taskChildInfo.getParams();
                         if(!StringUtils.isEmpty(params)){
                             String[] paramArr = params.split(",");
                             for(int i=0;i<paramArr.length;i++){
                                 String param=paramArr[i];
                                 DataFeatureDto dataFeatureDto=new DataFeatureDto();
                                 dataFeatureDto.setTaskId(taskChildInfo.getId());
                                 List<String> strList=new ArrayList<>();
                                 strList.add(param);
                                 dataFeatureDto.setParamCodes(strList);
                                 dataFeatureDto.setParamCode(taskChildInfo.getLegendName());
                                 dataFeatureDto.setStartTime(taskChildInfo.getStartTime());
                                 dataFeatureDto.setEndTime(taskChildInfo.getEndTime());
                                 dataFeatureDto.setSatelliteCode(taskChildInfo.getSatelliteCode());
                                 dataFeatureDto.setTaskName(taskInfo.getName()+'-'+taskChildInfo.getName()+'-'+param);
                                 dataFeatureDtoList.add(dataFeatureDto);
                             }
                         }
                     }
                }
            }
        }

        return dataFeatureDtoList;
    }




}
